package kook.domain.view;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Data
public class PageView {
    private int current;
    private int total;
    private Object data;

    public PageView() {
    }

    public PageView(int current, int total, Object data) {
        this.current = current;
        this.total = total;
        this.data = data;
    }

    public static <T> List<T> divide(List<T> list, int current, int size) {
        List<T> pageList = new ArrayList<>(size);
        if (current < 1) {
            current = 1;
        }
        if (size < 1) {
            size = 10;
        }
        int start = list.size() - (current - 1) * size - 1;
        int end = Math.max(start - size, -1);
        for (int i = start; i > end; i--) {
            pageList.add(list.get(i));
        }
        return pageList;
    }

    public static PageView create(List list, int current, int size) {
        List viewList = divide(list, current, size);
        PageView pageView = new PageView();
        pageView.setCurrent(current);
        pageView.setTotal(list.size());
        pageView.setData(viewList);
        return pageView;
    }

    @Override
    public String toString() {
        return "PageView("
                .concat("current=").concat(Objects.toString(current))
                .concat(", total=").concat(Objects.toString(total))
                .concat(", data=").concat(Objects.toString(data))
                .concat(")");
    }
}